package com.example.pingze.controller;

import com.example.pingze.entity.PoemInfo;
import com.example.pingze.entity.PoemUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoemInfoRepository;
import com.example.pingze.service.PoemUserCollectService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/poemUserCollect")
@CrossOrigin(origins = "*")
public class PoemUserCollectController {
    @Autowired
    private PoemUserCollectService service;
    @Autowired
    private PoemInfoRepository poemInfoRepository;
    //获取poemId和account，添加到诗词收藏表
    @PostMapping("/collect")
    public Result<String> collect(@RequestParam String account,
                                  @RequestParam String poemId){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(poemId == null){
            throw new PingZeException("诗词账号为空");
        }
        PoemUserCollect find = service.findPoemUserCollectByAccountAndPoemId(account, poemId);
        if(find == null){
            PoemUserCollect collect = new PoemUserCollect();
            collect.setAccount(account);
            collect.setPoemId(poemId);
            collect.setId(UUID.randomUUID().toString());
            service.collect(collect);
        }else {
            throw new PingZeException("该诗词已被收藏");
        }
        return ResultVOUtils.success("收藏成功");
    }
    //获取poemId和account，将用户的诗词收藏移除
    @PostMapping("/noCollect")
    public Result<String> noCollect(@RequestParam String account,
                                    @RequestParam String poemId){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(poemId == null){
            throw new PingZeException("诗词ID为空");
        }
        PoemUserCollect collect = new PoemUserCollect();
        collect.setAccount(account);
        collect.setPoemId(poemId);
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        List<PoemUserCollect> find = service.findPoemUserCollect(collect);
        if(find != null){
            for (PoemUserCollect list : find){
                service.delete(list);
            }
            return ResultVOUtils.success("取消收藏成功");
        }else{
            throw new PingZeException("用户的诗人信息未收藏");
        }
    }
    //多条件查询，收藏信息是否在收藏表，如果不在，则返回0
    @PostMapping("/getStatus")
    public Result<String> getStatus(@RequestParam String account,
                                    @RequestParam String poemId){
        String status = "";
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(poemId == null){
            throw new PingZeException("用户账号为空");
        }
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        PoemUserCollect find = service.findPoemUserCollectByAccountAndPoemId(account, poemId);
        if(find != null){
            status = "ok";
        }else{
            status = "no";
        }
        return ResultVOUtils.success(status);
    }
    //通过用户账号展示诗词列表
    @PostMapping("/getPoemList")
    public Result<List<PoemInfo>> getPoetList(@RequestParam String account){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        List<PoemUserCollect> poemUserCollectByAccount = service.findPoemUserCollectByAccount(account);
        if(poemUserCollectByAccount == null){
            throw new PingZeException("该用户该未收藏");
        }
        List<PoemInfo> poemInfoList = new ArrayList<>();
        //遍历查询出来的收藏信息
        for(PoemUserCollect poemUserCollect : poemUserCollectByAccount){
            String poemId = poemUserCollect.getPoemId();
            if(poemId != null){
                PoemInfo poemInfo = poemInfoRepository.findPoemInfoById(poemId);
                if(poemInfo != null){
                    poemInfoList.add(poemInfo);
                }
            }
        }
        return ResultVOUtils.result(poemInfoList);
    }
    /**
     * 后台管理系统
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<PoemUserCollect>> getList(
            PoemUserCollect poemUserCollect,
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order accountOrder = Sort.Order.asc("account");
        Sort sort = Sort.by(accountOrder);
        Pageable pageable = PageRequest.of(pageCode-1,pageSize,sort);
        Page page = service.getList(poemUserCollect,pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<PoemUserCollect> add(String account,String poemId){
        PoemUserCollect add = service.add(account,poemId);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<PoemUserCollect> edit(String id, String account, String poemId){
        PoemUserCollect edit = service.edit(id,account,poemId);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        PoemUserCollect find = service.findPoemUserCollectById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete successful");
    }
}
